<?php
//文章分类控制器
namespace Application\Admin\Controller;
use Application\Admin\Model\Article;
use Application\Admin\Model\Articlecat as artcat;
class Articlecat extends Admin {
	public function index() {
		$cat = new artcat();
		$this -> cat = $cat -> order('id asc') -> select();
		$this -> render();
	}

	public function add() {
		if (IS_POST) {
			$cat = new artcat();
			if($_POST['cat_name'] == ''){
				echo "<script>alert('请输入信息');
					window.location.href='index.php?m=admin&c=articlecat&a=add';</script>";
				exit ;
			}
			$cat -> attributes -> name = $_POST['cat_name'];
			if ($cat -> save()) {
				echo "<script>alert('添加成功');
					window.location.href='index.php?m=admin&c=articlecat&a=index';</script>";
				exit ;
			}
		}
		$this -> render('add');
	}

	public function delete() {
		if (IS_GET) {
			$id = $_GET['id'];
			$cat = new artcat();
			$cat = $cat -> findModel($id);
			if ($cat -> delete()) {
				echo "<script>alert('删除成功');
					window.location.href='index.php?m=admin&c=articlecat&a=index';</script>";
			}
		}
	}

	public function edit() {
		if (IS_GET) {
			$id = $_GET['id'];
			$cat = new artcat();
			$this -> cat = $cat -> find($id);
			$this -> render('edit');
		}
		if (IS_POST) {
			$catModel = new artcat();
			$id = $_POST['id'];
			$cat = $catModel -> findModel($id);
			$cat -> attributes -> name = $_POST['cat_name'];

			if ($cat -> save()) {
				echo "<script>alert('编辑成功');
					window.location.href='index.php?m=admin&c=articlecat&a=index';</script>";
			}
		}
	}

}
?>